﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;

public partial class myPayment : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    User user;
    PaymentHistory pHistory;
    List<PaymentHistory> pHistories = new List<PaymentHistory>();
    BankDetail bDetail;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["User"] != null)
            {
                BindPaymentHistories(Session["User"].ToString());
                fillBankDetails(Session["User"].ToString());
            }
            else
                Response.Redirect("default.aspx");
        }
    }
    private void fillBankDetails(string userName)
    {
        user = new User(_dataObject);
        user.UserName = userName;
        user.GetUserByUserName();

        if (user.Id != 0)
        {
            bDetail = new BankDetail(_dataObject);
            bDetail.UserId = user.Id;
            bDetail.GetBankDetailByUserId();

            if (bDetail.Id != 0)
            {
                txtBankName.Text = bDetail.Name;
                txtBankAccountNumber.Text = bDetail.AccountNumber;
                txtRoutingNumber.Text = bDetail.RoutingNumber;
            }
        }
    }
    private void BindPaymentHistories(string userName)
    {
        user = new User(_dataObject);
        user.UserName = userName;
        user.GetUserByUserName();

        if (user.Id != 0)
        {
            pHistory = new PaymentHistory(_dataObject);
            pHistory.UserId = user.Id;
            pHistories = pHistory.GetAllPaymentsByUserId();

            if (pHistories.Count > 0)
            {
                grdPayment.DataSource = pHistories;
                grdPayment.DataBind();
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        user = new User(_dataObject);
        user.UserName = Session["User"].ToString();
        user.GetUserByUserName();

        if (user.Id != 0)
        {
            BankDetail nDetail = new BankDetail(_dataObject);
            nDetail.UserId = user.Id;
            nDetail.GetBankDetailByUserId();

            bDetail = new BankDetail(_dataObject);
            bDetail.Name = txtBankName.Text;
            bDetail.AccountNumber = txtBankAccountNumber.Text;
            bDetail.RoutingNumber = txtRoutingNumber.Text;
            bDetail.UserId = user.Id;

            if (nDetail.Id != 0)
            {
                bDetail.Id = nDetail.Id;
                bDetail.Update();
            }
            else
                bDetail.Create();

            fillBankDetails(Session["User"].ToString());
        }
    }
}